package indi.zhifa.study2024.nbr.business.curd.item.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import indi.zhifa.core.common.util.DtoEntityUtil;
import indi.zhifa.core.common.web.entity.exception.ServiceException;
import indi.zhifa.core.mpenhance.utils.DbDtoEntityUtil;
import indi.zhifa.study2024.nbr.business.curd.item.dao.service.IItemDefineDbService;
import indi.zhifa.study2024.nbr.business.curd.item.entity.po.ItemDefineEntity;
import indi.zhifa.study2024.nbr.business.curd.item.entity.request.ItemDefineCreateRequest;
import indi.zhifa.study2024.nbr.business.curd.item.entity.request.ItemDefineEditRequest;
import indi.zhifa.study2024.nbr.business.curd.item.entity.request.ItemPageRequest;
import indi.zhifa.study2024.nbr.business.curd.item.entity.response.ItemDefineEntityVo;
import indi.zhifa.study2024.nbr.business.curd.item.service.IItemDefineService;
import indi.zhifa.study2024.nbr.business.curd.storage.dao.service.IItemInventoryDbService;
import indi.zhifa.study2024.nbr.business.curd.storage.entity.po.ItemInventoryEntity;
import indi.zhifa.study2024.nbr.enums.item.EItemUnitType;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;

@RequiredArgsConstructor
@Service
public class ItemDefineServiceImpl implements IItemDefineService {

    private final IItemDefineDbService mItemDefineDbService;
    private final IItemInventoryDbService mItemInventoryDbService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public ItemDefineEntityVo create(ItemDefineCreateRequest pItemDefineCreateRequest) {

        ItemDefineEntity itemDefineEntity = mItemDefineDbService.findByName(pItemDefineCreateRequest.getName());
        if(null != itemDefineEntity) {
            throw new ServiceException("已存在名为"+pItemDefineCreateRequest.getName()+"的商品，Id为"+itemDefineEntity.getId());
        }

        itemDefineEntity = DbDtoEntityUtil.createFromDto(pItemDefineCreateRequest, ItemDefineEntity.class);
        // 为了简化，直接上架吧
        itemDefineEntity.setOnShelves(true);
        if(itemDefineEntity.getUnitType() == EItemUnitType.SINGLE_UNIT){
            itemDefineEntity.setLargeConvert(1);
        }


        mItemDefineDbService.save(itemDefineEntity);
        // 创建库存信息
        ItemInventoryEntity itemInventoryEntity = DbDtoEntityUtil.createFromDto(itemDefineEntity, ItemInventoryEntity.class);
        itemInventoryEntity.setItemId(itemDefineEntity.getId());
        itemInventoryEntity.setItemName(itemDefineEntity.getName());

        // 这里放大单位价格
        itemInventoryEntity.setCurPriceLarge(pItemDefineCreateRequest.getNormalPrice());
        itemInventoryEntity.setCurPrice(pItemDefineCreateRequest.getNormalPrice()
                .divide(BigDecimal.valueOf(itemDefineEntity.getLargeConvert()),4, RoundingMode.HALF_UP));
        itemInventoryEntity.setCurPriceLarge(pItemDefineCreateRequest.getNormalPrice());

        if(pItemDefineCreateRequest.getIniStorage() != null && pItemDefineCreateRequest.getIniStorage() > 0){
            itemInventoryEntity.setStorage(pItemDefineCreateRequest.getIniStorage() * itemDefineEntity.getLargeConvert());
        }else{
            itemInventoryEntity.setStorage(0l);
        }


        mItemInventoryDbService.save(itemInventoryEntity);

        ItemDefineEntityVo vo = DtoEntityUtil.trans(itemDefineEntity, ItemDefineEntityVo.class);
        return vo;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public ItemDefineEntityVo update(Long pId, ItemDefineEditRequest pItemDefineEditRequest) {
        ItemDefineEntity itemDefineEntity = mItemDefineDbService.check(pId);
        ItemDefineEntity newItemDefineEntity = DbDtoEntityUtil.editByDto(itemDefineEntity,pItemDefineEditRequest, ItemDefineEntity.class);
        mItemDefineDbService.updateById(newItemDefineEntity);
        ItemDefineEntityVo vo = DtoEntityUtil.trans(newItemDefineEntity, ItemDefineEntityVo.class);
        return vo;
    }

    @Override
    public ItemDefineEntityVo info(Long pId) {
        ItemDefineEntity itemDefineEntity = mItemDefineDbService.check(pId);
        ItemDefineEntityVo vo = DtoEntityUtil.trans(itemDefineEntity, ItemDefineEntityVo.class);
        return vo;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public ItemDefineEntityVo delete(Long pId) {
        ItemDefineEntity itemDefineEntity = mItemDefineDbService.check(pId);
        mItemDefineDbService.deleteById(pId);
        ItemDefineEntityVo vo = DtoEntityUtil.trans(itemDefineEntity, ItemDefineEntityVo.class);
        return vo;
    }

    @Override
    public Page<ItemDefineEntityVo> page(ItemPageRequest pItemPageRequest) {

        LambdaQueryWrapper<ItemDefineEntity> queryWrapper = Wrappers.<ItemDefineEntity>lambdaQuery()
                .like(StringUtils.hasText(pItemPageRequest.getItemName()),ItemDefineEntity::getName,pItemPageRequest.getItemName())
                .eq(StringUtils.hasText(pItemPageRequest.getBrandName()),ItemDefineEntity::getBrand,pItemPageRequest.getBrandName())
                .eq(StringUtils.hasText(pItemPageRequest.getMainType()),ItemDefineEntity::getMainType,pItemPageRequest.getMainType())
                .eq(StringUtils.hasText(pItemPageRequest.getSegmentType()),ItemDefineEntity::getSegmentType,pItemPageRequest.getSegmentType());
        Page<ItemDefineEntity> pageCfg = new Page<ItemDefineEntity>(pItemPageRequest.getCurrent(),pItemPageRequest.getSize());
        return mItemDefineDbService.page(pageCfg,queryWrapper,ItemDefineEntityVo.class);
    }
}
